//
//  MainViewController.swift
//  SwiftSina
//
//  Created by 刘战军 on 16/5/10.
//  Copyright © 2016年 LiuZhanJun. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    // MARK: - 重写 viewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 添加子控制器
        addChildViewControllers()
        
        setupTabBar()
    }
    
    // MARK: - 重写 viewWillAppear 设置按钮的frame
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 将按钮置顶
        tabBar.bringSubviewToFront(centerButton)
        
        // 设置按钮的frmae
        // 计算tabBar中控件的位置
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        
        // 设置按钮的frmae
        centerButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    // MARK: - 私有控件, 添加中间的按钮
    // 懒加载一个Button
    private lazy var centerButton: UIButton = UIButton(title: nil, imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
//    {
//        
//        let button = UIButton()
//        let imageNameBack = "tabbar_compose_icon_add"
//        button.setImage(UIImage(named: imageNameBack), forState: .Normal)
//        button.setImage(UIImage(named: imageNameBack + "_highlighted"), forState: .Highlighted)
//        
//        let imageName = "tabbar_compose_button"
//        button.setBackgroundImage(UIImage(named: imageName), forState: .Normal)
//        button.setBackgroundImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
//        
//        // 让按钮的大小和图片的大小一样大
//        button.sizeToFit()
//        
//        return button
//    }()
    
}

// MARK: - 设置tabBar
extension MainViewController {
    
    private func setupTabBar() {

        // 设置tabbar背景图片, 以下两句代码必须连用, Nav 的处理类似
        // 以下两个属性都设置成[[UIImage alloc] init]就是完全透明
        // 如果提供图像, 会自动拉伸
        tabBar.shadowImage = UIImage.zj_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        // 添加撰写按钮
        tabBar.addSubview(centerButton)
    }
}


// MARK: - 添加控制器
// 设置 extension 将相关的代码进行了分组
extension MainViewController {
    
    // 动态添加子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        
        addChildViewController(UIViewController())
        
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_home")
        
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_discover")
    }
    
    
    // 创建一个添加控制器的方法, 方便外界调用
    private func addChildViewController(Vc: UIViewController, title: String, imageName: String) {
        
        Vc.title = title;
        
        Vc.tabBarItem.image = UIImage(named: imageName)
        
        // 设置文本属性, 设置文字的颜色
        // Vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        // 设置字体大小
        Vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        
        // 默认系统会渲染我们的图片的颜色, 需要加AlwaysOriginal渲染的模式, 让系统不要渲染我们素材原本的颜色
        Vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected") // ?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: Vc)
        
        addChildViewController(nav)
        
    }

}